package com.xkdx.caipiao.my;

import java.util.HashMap;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.register.RegisterAction;
import com.xkdx.caipiao.presistence.register.RegisterModule;
import com.xkdx.caipiao.presistence.register.RegisterPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class RegisterActivity extends ThreadActivity {
	TextView titel_info, tv_protocol;
	EditText userName, psw;
	Button register;
	RegisterAction action;
	RegisterModule module;
	RegisterPresistence presistence;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.register_caipiao);
		findView();
		setClick();

	}

	private void setClick() {
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(userName.getText().toString())){
					Toast.makeText(RegisterActivity.this, "手机号不能为空", 1).show();
					return;
				}else if(userName.getText().toString().length()<11){
					Toast.makeText(RegisterActivity.this, "请输入正确的手机号", 1).show();
					return;
				}
				if (TextUtils.isEmpty(psw.getText().toString())) {
					Toast.makeText(RegisterActivity.this, "密码不能为空", 1).show();
					return;
				} if (psw.getText().toString().length() < 6 ) {
					Toast.makeText(RegisterActivity.this, "密码长度为6—12位,请重新输入", 1).show();
					return;
				}
				startThread();
			}
			
			
		});
		tv_protocol.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(RegisterActivity.this,
						AboutProtocolActivtiy.class);
				startActivity(intent);
			}
		});

	}

	private void findView() {
		titel_info = (TextView) findViewById(R.id.title_info);
		titel_info.setText("注册");
		tv_protocol = (TextView) findViewById(R.id.tv_protocol);
		SpannableStringBuilder style = new SpannableStringBuilder(
				getResources().getString(R.string.pay_double_rule_tip));
		style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, style.length(),
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		tv_protocol.setText(style);
		titel_info.setVisibility(View.GONE);
		userName = (EditText) findViewById(R.id.et_user_name);
		psw = (EditText) findViewById(R.id.et_user_psw);
		register = (Button) findViewById(R.id.btn_register);
	}

	public void onBackClick(View view) {
		finish();
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			if (module.isRegisterSuccess) {
				saveUserInfo();
				setResult(301);
				finish();
			}
		} else {
			Toast.makeText(this, "用户账号重复", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void startThread() {

		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		String md5 = userName.getText().toString()+psw.getText().toString()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new RegisterAction(userName.getText().toString(), psw
				.getText().toString(), "2",signcode);
		module = new RegisterModule();
		presistence = new RegisterPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}

	private void saveUserInfo() {
		SharePrefenceUtil su = new SharePrefenceUtil(this,
				SharePrefenceUtil.USERINFO);
		su.saveObject(SharePrefenceUtil.USERINFO, module.loginInfo);

	}

}
